org 0x0000
	
	ori	$sp, $0, 0x6000 #new stack location
	addu    $fp, $sp, $0
	jal main2
	nop

	main2:
	
	addiu	$sp,$sp,-32
	#sw	$fp,28($sp)
	addu	$fp,$sp,$0

#	ori 	$2, $0, 50
	sw	$0,12($fp) #j
	sw	$0,16($fp) #i
	
	j	L22
	nop
	
	L62:

#	ori 	$2, $0, 50
	sw	$0,12($fp) #reset j check inner loop bounds
	j	L32
	nop
	
	L52: #the meat

	
	lw	$2,12($fp) #j
	nop
	sll	$2,$2,2  # words
#	addiu	$3,$fp,8 #offset to start of array
	ori     $3, $0, sortdata
	addiu 	$3, $3, 200 # offset 50 into the array
	addu	$2,$3,$2 #data[j]
	lw	$3,0($2)
	lw	$2,12($fp)
	nop
	addiu	$2,$2,1
	sll	$2,$2,2
	#addiu	$4,$fp,8
	ori     $4, $0, sortdata
	addiu 	$4, $4, 200 # offset 50 into the array
	addu	$2,$4,$2
	lw	$2,0($2)
	nop
	slt	$2,$2,$3
	beq	$2,$0,L42
	nop
	
	lw	$2,12($fp)
	nop
	addiu	$2,$2,1
	slti	$2,$2,50
	beq	$2,$0,L42
	nop
	
	lw	$2,12($fp)
	nop
	sll	$2,$2,2
#	addiu	$3,$fp,8
	ori     $3, $0, sortdata
	addiu 	$3, $3, 200 # offset 50 into the array
	addu	$2,$3,$2
	lw	$2,0($2) #!!!!!!!!!!!!!!!!!!!!!!!!
	nop
	sw	$2,8($fp)
	
	ori     $3, $0, sortdata
	addiu 	$3, $3, 200 # offset 50 into the array
	
	lw	$4,12($fp) #also j #!!!!!!!!!!!!!!!!!!!!!!!! /end
	lw	$2,12($fp) #this is j?
	nop
	addiu	$2,$2,1 #increment
	sll	$2,$2,2 #so get the word offset of j
#	addiu	$3,$fp,8 #so fp + 8 looks at ?????
	
	ori 	$3, $0, sortdata
	addiu 	$3, $3, 200 # offset 50 into the array
	
	addu	$2,$3,$2 #go that far into an array of some sort.
	lw	$3,0($2)#okay extra 12 here. so 12 + 8 + word offset = 20+ offset = array[j]
	sll	$2,$4,2 # j *4 
#	addiu	$4,$fp,8

	ori	$4, $0, sortdata
	addiu 	$4, $4, 200 # offset 50 into the array
	addu	$2,$4,$2
	sw	$3,0($2)#store value
	lw	$2,12($fp)#load j
	nop
	addiu	$2,$2,1
	sll	$2,$2,2
#	addiu	$3,$fp,8
	ori     $3,$0,sortdata
	addiu 	$3, $3, 200 # offset 50 into the array
	
	addu	$2,$3,$2
	lw	$3,8($fp)
	nop
	sw	$3,0($2)


        L42:

	
	lw	$2,12($fp)
	nop
	addiu	$2,$2,1
	sw	$2,12($fp)
	
	L32:

	
	ori	$3, $zero, 50
	lw	$2,16($fp)
	nop
	subu	$3,$3,$2
	lw	$2,12($fp)
	nop
	slt	$2,$2,$3
	bne	$2,$0,L52
	nop
	
	lw	$2,16($fp)
	nop
	addiu	$2,$2,1
	sw	$2,16($fp)
	
	L22: #outer loop check.. a fail od inner loop check will fall throughh to here

	
	lw	$2,16($fp)
	nop
	slti	$2,$2,50
	bne	$2,$0,L62
	nop
	
	addu	$2,$0,$0
	addu	$sp,$fp,$0
	lw	$fp,28($sp)
	addiu	$sp,$sp,32

finished1:	
	ori $2, $0, doneflag
	ll $3, 0($2)
	bne $3, $0, mergecode
	addiu $3, $3, 1
	sc $3, 0($2)
	beq $3, $0, finished1
	
        halt

org 0x200
	
	ori	$sp, $0, 0x7000 #new stack location
	addu    $fp, $sp, $0
	jal main1
	nop

	main1:
	
	addiu	$sp,$sp,-32
	#sw	$fp,28($sp)
	addu	$fp,$sp,$0

	sw	$0,12($fp) #j
	sw	$0,16($fp) #i
	
	j	L21
	nop
	
	L61:
	
	sw	$0,12($fp) #reset j check inner loop bounds
	j	L31
	nop
	
	L51: #the meat

	
	lw	$2,12($fp) #j
	nop
	sll	$2,$2,2  # words
#	addiu	$3,$fp,8 #offset to start of array
	ori     $3, $0, sortdata
	addu	$2,$3,$2 #data[j]
	lw	$3,0($2)
	lw	$2,12($fp)
	nop
	addiu	$2,$2,1
	sll	$2,$2,2
	#addiu	$4,$fp,8
	ori     $4, $0, sortdata
	addu	$2,$4,$2
	lw	$2,0($2)
	nop
	slt	$2,$2,$3
	beq	$2,$0,L41
	nop
	
	lw	$2,12($fp)
	nop
	addiu	$2,$2,1
	slti	$2,$2,50
	beq	$2,$0,L41
	nop
	
	lw	$2,12($fp)
	nop
	sll	$2,$2,2
#	addiu	$3,$fp,8
	ori     $3, $0, sortdata
	addu	$2,$3,$2
	lw	$2,0($2) #!!!!!!!!!!!!!!!!!!!!!!!!
	nop
	sw	$2,8($fp)
	
	ori     $3, $0, sortdata
	lw	$4,12($fp) #also j #!!!!!!!!!!!!!!!!!!!!!!!! /end
	lw	$2,12($fp) #this is j?
	nop
	addiu	$2,$2,1 #increment
	sll	$2,$2,2 #so get the word offset of j
#	addiu	$3,$fp,8 #so fp + 8 looks at ?????
	ori 	$3, $0, sortdata
	addu	$2,$3,$2 #go that far into an array of some sort.
	lw	$3,0($2)#okay extra 12 here. so 12 + 8 + word offset = 20+ offset = array[j]
	sll	$2,$4,2 # j *4 
#	addiu	$4,$fp,8
	ori	$4, $0, sortdata
	addu	$2,$4,$2
	sw	$3,0($2)#store value
	lw	$2,12($fp)#load j
	nop
	addiu	$2,$2,1
	sll	$2,$2,2
#	addiu	$3,$fp,8
	ori     $3,$0,sortdata
	addu	$2,$3,$2
	lw	$3,8($fp)
	nop
	sw	$3,0($2)


        L41:

	
	lw	$2,12($fp)
	nop
	addiu	$2,$2,1
	sw	$2,12($fp)
	
	L31:

	
	ori	$3, $zero, 50
	lw	$2,16($fp)
	nop
	subu	$3,$3,$2
	lw	$2,12($fp)
	nop
	slt	$2,$2,$3
	bne	$2,$0,L51
	nop
	
	lw	$2,16($fp)
	nop
	addiu	$2,$2,1
	sw	$2,16($fp)
	
	L21: #outer loop check.. a fail od inner loop check will fall throughh to here

	
	lw	$2,16($fp)
	nop
	slti	$2,$2,50
	bne	$2,$0,L61
	nop
	
	addu	$2,$0,$0
	addu	$sp,$fp,$0
	lw	$fp,28($sp)
	addiu	$sp,$sp,32


finished2:	
	ori $2, $0, doneflag
	ll $3, 0($2)
	bne $3, $0, mergecode
	addiu $3, $3, 1
	sc $3, 0($2)
	beq $3, $0, finished2
			
       halt
	
#	okay lets do some merging eh?
mergecode:	
	ori $2, $0, sortdata #  &data[0]
	ori $3, $0, sortdata
	addiu $3, $3, 200    #  &data[50]
	ori $4, $0, 0 #index for 1(add 4 each time, check against 200)
	ori $5, $0, 0 #index for 5(add 4 each time, check against 200)
	ori $6, $0, 0 #comparison

mergeloop:
	addu $7, $2, $4
	lw  $7, 0($7)
	addu $8, $3, $5
	lw  $8, 0($8)
	slt $6, $7, $8
	beq $6, $0, branch2
# first half is less
	ori $9, $0, sorteddata
	addu $9, $9, $4
	addu $9, $9, $5
	sw $7, 0($9)
	addiu $4, $4, 4
	slti $6, $4, 200
	bne $6, $0, mergeloop

	#okay so our counter for 1 was 200 ... need to loop through rest of 2
mergeclean1:
	addu $8, $3, $5
	lw  $8, 0($8)
	ori $9, $0, sorteddata
	addu $9, $9, $4
	addu $9, $9, $5
	
	sw $8, 0($9)
	addiu $5, $5, 4
	slti $6, $5, 200
	bne $6, $0, mergeclean1
	
	j mergefinish

branch2:	
# second half is less
	ori $9, $0, sorteddata
	addu $9, $9, $4
	addu $9, $9, $5
	sw $8, 0($9)
	addiu $5, $5, 4
	slti $6, $5, 200
	bne $6, $0, mergeloop

	#okay so our counter for 1 was 200 ... need to loop through rest of 2
mergeclean2:
	addu $7, $2, $4
	lw  $7, 0($7)
	ori $9, $0, sorteddata
	addu $9, $9, $4
	addu $9, $9, $5
	
	sw $7, 0($9)
	addiu $4, $4, 4
	slti $6, $4, 200
	bne $6, $0, mergeclean2
	
	j mergefinish







mergefinish:	
	halt

	
org 0x2000


sortdata:	
cfw 0x087d
cfw 0x5fcb
cfw 0xa41a
cfw 0x4109
cfw 0x4522
cfw 0x700f
cfw 0x766d
cfw 0x6f60
cfw 0x8a5e
cfw 0x9580
cfw 0x70a3
cfw 0xaea9
cfw 0x711a
cfw 0x6f81
cfw 0x8f9a
cfw 0x2584
cfw 0xa599
cfw 0x4015
cfw 0xce81
cfw 0xf55b
cfw 0x399e
cfw 0xa23f
cfw 0x3588
cfw 0x33ac
cfw 0xbce7
cfw 0x2a6b
cfw 0x9fa1
cfw 0xc94b
cfw 0xc65b
cfw 0x0068
cfw 0xf499
cfw 0x5f71
cfw 0xd06f
cfw 0x14df
cfw 0x1165
cfw 0xf88d
cfw 0x4ba4
cfw 0x2e74
cfw 0x5c6f
cfw 0xd11e
cfw 0x9222
cfw 0xacdb
cfw 0x1038
cfw 0xab17
cfw 0xf7ce
cfw 0x8a9e
cfw 0x9aa3
cfw 0xb495
cfw 0x8a5e
cfw 0xd859
cfw 0x0bac
cfw 0xd0db
cfw 0x3552
cfw 0xa6b0
cfw 0x727f
cfw 0x28e4
cfw 0xe5cf
cfw 0x163c
cfw 0x3411
cfw 0x8f07
cfw 0xfab7
cfw 0x0f34
cfw 0xdabf
cfw 0x6f6f
cfw 0xc598
cfw 0xf496
cfw 0x9a9a
cfw 0xbd6a
cfw 0x2136
cfw 0x810a
cfw 0xca55
cfw 0x8bce
cfw 0x2ac4
cfw 0xddce
cfw 0xdd06
cfw 0xc4fc
cfw 0xfb2f
cfw 0xee5f
cfw 0xfd30
cfw 0xc540
cfw 0xd5f1
cfw 0xbdad
cfw 0x45c3
cfw 0x708a
cfw 0xa359
cfw 0xf40d
cfw 0xba06
cfw 0xbace
cfw 0xb447
cfw 0x3f48
cfw 0x899e
cfw 0x8084
cfw 0xbdb9
cfw 0xa05a
cfw 0xe225
cfw 0xfb0c
cfw 0xb2b2
cfw 0xa4db
cfw 0x8bf9
cfw 0x12f7

org 0x02500
doneflag:	
cfw 0x0000
	
org 0x03000
sorteddata:
#so the next 400 mem addresses will be the sorted result array
